//
//  ST_TodayTraitHeaderReusableView.swift
//  btvideo
//
//  Created by mac on 2020/3/21.
//  Copyright © 2020 JarvanZhang. All rights reserved.
//

import UIKit
import SnapKit
import SwifterSwift



/**
 Today 样式
 */
class ST_TodayTraitHeaderReusableView: UICollectionReusableView {
        
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        
        self.addSubview(cover_image_view)
        
        cover_image_view.snp.makeConstraints { (make) in
            make.edges.equalToSuperview().inset(UIEdgeInsets.init(top: 0, left: 0, bottom: 20, right: 0))
        }
        
        self.addSubview(topMaskView)
        
        topMaskView.snp.makeConstraints { (make) in
            make.top.right.left.equalToSuperview()
            make.height.equalTo(110+statusBarHeight-20)
        }
        
        self.addSubview(titleLabel)
        self.addSubview(content_label)
        
        
        
        titleLabel.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(25)
            make.top.equalToSuperview().offset(43-20+statusBarHeight)
        }
        
        content_label.snp.makeConstraints { (make) in
            make.left.equalTo(titleLabel)
            make.top.equalTo(titleLabel.snp.bottom).offset(8)
        }
  
        
        self.addSubview(click_btn)
        click_btn.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.bottom.equalToSuperview().offset(-20-20)
            make.height.equalTo(40)
            make.width.equalTo(200)
        }
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    fileprivate lazy var cover_image_view:UIImageView = {
        let control = UIImageView()
        control.contentMode = UIView.ContentMode.scaleAspectFill
        control.clipsToBounds = true
        return control
    }()
    
    fileprivate lazy var topMaskView:ST_FWGradientView = {
       let control = ST_FWGradientView()
        control.bgLayer.colors = [UIColor.init(hex: 0x040813, transparency: 0.5)!.cgColor, UIColor.clear.cgColor]
       control.bgLayer.locations = [0, 1]
       control.bgLayer.startPoint = CGPoint(x: 0, y: 0)
       control.bgLayer.endPoint = CGPoint(x: 0, y: 1)
       return control
   }()
    
    fileprivate lazy var titleLabel:UILabel = {
        let control = UILabel.init()
        control.text = ""
        control.textColor = Color.white
        //        control.numberOfLines = 0
        //        control.textAlignment = .center
        control.font = UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.semibold)
        return control
    }()
    
    fileprivate lazy var content_label:UILabel = {
        let control = UILabel.init()
        control.text = ""
        control.textColor = Color.white
        //        control.numberOfLines = 0
        control.font = UIFont.systemFont(ofSize: 22, weight: UIFont.Weight.semibold)
        return control
    }()
    
    
//    喜欢
    fileprivate lazy var selectedBackgroundImage = UIImage.init(color: Color.init(hex: 0xED3E58)!, size: CGSize.init(width: 1, height: 1))
//    添加
    fileprivate lazy var normalBackgroundImage = UIImage.init(color: Color.init(hex: 0xFEC207)!, size: CGSize.init(width: 1, height: 1))
//    disable
    fileprivate lazy var disableBackgroundImage = UIImage.init(color: Color.init(hex: 0xE0E5EC)!, size: CGSize.init(width: 1, height: 1))
    
    
    fileprivate lazy var click_btn:UIButton = {
        
        let control = UIButton.init(type: UIButton.ButtonType.custom)
        
        control.setBackgroundImage(disableBackgroundImage, for: UIControl.State.disabled)
        
        control.setBackgroundImage(normalBackgroundImage, for: UIControl.State.normal)
        
        control.setBackgroundImage(selectedBackgroundImage, for: UIControl.State.selected)
        
        control.setTitleColorForAllStates(Color.white)
        
        control.setTitleColor(Color.init(hex: 0x61666F), for: UIControl.State.disabled)
        
        control.setTitle("添加特质", for: UIControl.State.normal)
        
        control.setTitle("喜欢特质", for: UIControl.State.selected)
        
        control.setTitle("已喜欢", for: UIControl.State.disabled)
        
        control.cornerRadius = 20
        
        control.titleLabel?.font = UIFont.systemFont(ofSize: 18, weight: UIFont.Weight.semibold)
        
        control.addTarget(self, action: #selector(click_handle(sender:)), for: UIControl.Event.touchUpInside)
        
        control.isHidden = true
        return control
    }()
    
//    MARK: - Bind Model
    var parent_peculiarity = ST_PeculiarityModel(){
        didSet{
            
            
            cover_image_view.pin_setImage(from: parent_peculiarity.image.compress_image().url, placeholderImage: ST_SwiftConst.shared.fetch_random_place_holder_image())
            
            titleLabel.text = parent_peculiarity.name
            content_label.text = parent_peculiarity.descriptionStr
            
            
            var loveStr = ""
//            特质是男
            if parent_peculiarity.type == 1{
//                自己是女
                if ST_BTUserInfoManager.shared.current_account_info?.userinfo?.sex != ST_SEX.male{
//                    多少男生拥有
                    loveStr = String.init(format: "%@个男生拥有",parent_peculiarity.own_num.st_number_description())
                }
//               自己是男
                else{
//                    多少女生喜欢
                    loveStr = String.init(format: "%@个女生喜欢",parent_peculiarity.like_num.st_number_description())
                }
            }
//            特质是女
            else{
//                  自己是女
                if ST_BTUserInfoManager.shared.current_account_info?.userinfo?.sex != ST_SEX.male{
//                    多少男生喜欢
                    loveStr = String.init(format: "%@个男生喜欢",parent_peculiarity.like_num.st_number_description())
                }
//               自己是男
                else{
//                    多少女生拥有
                  loveStr = String.init(format: "%@个女生拥有",parent_peculiarity.own_num.st_number_description())
                    
                }
            }
            
            content_label.text = loveStr
            
            
            if parent_peculiarity.authJson["type"].intValue == 1{
//                已喜欢
                if parent_peculiarity.authJson["is_like"].boolValue{
                    click_btn.isEnabled = false
                    self.click_btn.isSelected = false
                }
//               喜欢特质
                else{
                    click_btn.isEnabled = true
                    click_btn.isSelected = true
                }
            }
//            添加特质
            else if parent_peculiarity.authJson["type"].intValue == 2{
                click_btn.isEnabled = true
                click_btn.isSelected = false
            }
            
            main_thread_execute(afterDelay: 0.75) {
                self.click_btn.isHidden = false
            }
            
        }
    }
    
    
    
}


extension ST_TodayTraitHeaderReusableView{
    
    @objc func click_handle(sender:UIButton){
        
//        [写喜欢]点击事件
        if sender.isSelected{
            
            JVrequest(PH.likePeculiarityApi,parameters: ["feature_id":parent_peculiarity.id], success: { (json) in
                
                self.click_btn.isEnabled = false
                self.click_btn.isSelected = false
                
                
                NotificationCenter.default.post(name: Notification.Name.like_peculiarity, object: nil)
                
            }) { (code, err) in
                
                guard let view = self.parentViewController?.view else{return}
                ToastHud(text: err, view: view)
            }
        }
//        添加特质
        else{
         
            willPushPeculiarity = parent_peculiarity
            
            push_peculiarity(navigationController: self.parentViewController?.navigationController, operationController: ST_PushPeculiaritySelectMediaController())
            
        }
        
    }
}
